#!/usr/bin/env python
#coding=utf-8

import requests

jsonrpc_url = 'http://localhost:5000/jsonrpc'

def call(method:str, param:dict|list|object|None = None, rpc_id:str|None = None):

    response = requests.post(
        jsonrpc_url,
        json={'jsonrpc':'2.0', 'method':method, 'params':param, 'id':rpc_id},
        headers={'Content-Type': 'application/json'}
    )
    # print(response.text)
    return response

# def call_add(a:int, b:int) -> int:
#     response = call('add', a, b)
#     if response.status_code != 200 or 'error' in response.json().keys():
#         raise RuntimeError(response.json().get('error'))
#     return int(response.json().get('result'))

def call_add(a:int, b:int) -> int:
    return call('add', [a, b]).json().get('result')

def call_mul(a:int, b:int) -> int:
    return call('mul', [a, b]).json().get('result')

def call_square(a:int) -> int:
    return call('square', a).json().get('result')

def call_greet() -> str:
    return call('greet').json().get('result')

def call_raise(a:int) -> dict:
    return call('raise', a).json()

def call_crit() -> dict:
    return call('critical').json()

if __name__ == '__main__':
    print("call_add(1,2)", call_add(1,2))
    print("call_mul(2,3)", call_mul(2,3))
    print("call_square(25)", call_square(25))
    print("call_greet()", call_greet())
    print("call_crit()", call_crit())
    print("call_raise(12)", call_raise(12))